<?php

namespace app\common\model;

/**
 * 文章模型
 * Class Article
 * @package app\common\model
 */
class Page extends BaseModel
{
    protected $name = 'page';
    //protected $append = ['show_views'];


    /**
     * 展示的浏览次数
     * @param $value
     * @param $data
     * @return mixed
     */
    public function getShowViewsAttr($value, $data)
    {
        return $data['virtual_views'] + $data['actual_views'];
    }

    /**
     * 文章详情
     * @param $article_id
     * @return null|static
     * @throws \think\exception\DbException
     */
    public static function detail($article_id)
    {
        return self::get($article_id, ['image']);
    }


    /**
     * 关联文章封面图
     * @return \think\model\relation\HasOne
     */
    public function image()
    {
        return $this->hasOne('uploadFile', 'file_id', 'image_id');
    }

    /**
     * 文章列表
     * @param $id
     */
    public function getArticle($where = [],$limit = '',$paginate=false)
    {

        $where['is_delete'] = 0;
        if(!$paginate){
            
            return $this->with('image')->field('article_id,article_title,article_subtitle,article_content,update_time,image_id,create_time')->where($where)->order('article_sort asc,update_time desc,create_time desc')->limit($limit)->select();
        }

        return $this->with('image')->field('article_id,article_title,article_subtitle,article_content,update_time,image_id')->where($where)->order('article_sort asc,update_time desc,create_time desc')->limit($limit)->paginate(10, false, [
            'query' => request()->request()
        ]);
    }

}
